package controller;

import model.Message;
import model.SocketMap;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.BindException;
import java.net.Socket;
import java.net.SocketException;

public class TClient implements Runnable {

    Socket socket = null;
    ObjectInputStream is = null;
    ObjectOutputStream os = null;
    Socket mysocket;

    TClient(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            while (true) {
                is = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) is.readObject();
                System.out.println(message.getMessage());
                SocketMap socketMap = new SocketMap();
                mysocket = socketMap.getSocket(message.getGet_id());
                os = new ObjectOutputStream(mysocket.getOutputStream());
                os.writeObject(message);
            }

        } catch (SocketException e) {
            System.out.println("对方退出了");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}

